﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace FireTVcontrol
{
    public static class ClassAdb
    {
        private static Process adbProcess;
        private static StringBuilder stdOutBuffer = null;
        //private static string ip = String.Empty;
        public static string startupFolder = String.Empty;

        public static bool Connect(string ip, ref string msg)
        {
            bool rv = true;
            string cmd = "connect " + ip;
            rv = AdbCommand(cmd, ref msg);
            if (!(msg.ToLower().Contains("connected to " + ip)))
            {
                rv = false;
            }
            return rv;
        }

        public static bool DisConnect(ref string msg)
        {
            bool rv = true;
            string cmd = "disconnect ";
            rv = AdbCommand(cmd, ref msg);
            if (rv)
            {
                rv = AdbCommand("kill-server", ref msg);
            }
            return rv;
        }


        private static void CreateProcess()
        {
            adbProcess = new Process();
            adbProcess.StartInfo.FileName = startupFolder + "adb.exe";
            adbProcess.StartInfo.UseShellExecute = false; // enables output redirection
            adbProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            adbProcess.StartInfo.CreateNoWindow = true;
            adbProcess.StartInfo.RedirectStandardOutput = true;
            adbProcess.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
            adbProcess.StartInfo.WorkingDirectory = startupFolder;
            stdOutBuffer = new StringBuilder();
        }

        private static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            if (!String.IsNullOrEmpty(outLine.Data))
            {
                stdOutBuffer.AppendLine(outLine.Data);
            }
        }

        public static bool AdbCommand(string cmd, ref string response)
        {
            bool rv = true;
            CreateProcess();
            adbProcess.StartInfo.Arguments = cmd;
            try
            {
                rv = adbProcess.Start();
            }
            catch (Exception e)
            {
                rv = false;
                response = "adb.exe: " + e.Message;
                return rv;
            }
            int loop = 0;
            if (rv)
            {
                adbProcess.BeginOutputReadLine();
                do
                {
                    Thread.Sleep(20);
                    loop++;
                }
                //while (loop < 300);
                while (!adbProcess.HasExited && (loop < 300));
                if (loop < 300)
                {
                    response = stdOutBuffer.ToString();
                    if (String.IsNullOrEmpty(response))
                    {
                        response = "(no response)";
                    }
                }
                else
                {
                    try
                    {
                        adbProcess.Kill();
                    }
                    catch
                    {
                    }
                    rv = false;
                    response = "Command \"" + cmd + "\" timed out";
                }
            }
            else
            {
                response = "adb process failed to start";
            }
            return rv;
        }


    }
}
